import { Effect } from 'dva';
import { Reducer } from 'redux';
import { queryCurrent, query as queryUsers, getAccountMenus } from '@/services/user';
import { User as CurrentUser } from './data';
import { routerRedux } from 'dva/router';
import {message} from "antd";

// export interface CurrentUser {
//   avatar?: string;
//   name?: string;
//   title?: string;
//   group?: string;
//   signature?: string;
//   tags?: {
//     key: string;
//     label: string;
//   }[];
//   userid?: string;
//   unreadCount?: number;
// }

export interface UserModelState {
  currentUser?: CurrentUser;
  menuData?: any[];
  isFetchMenu: boolean;
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    fetch: Effect;
    fetchCurrent: Effect;
    getMenuData: Effect;
  };
  reducers: {
    saveCurrentUser: Reducer<{}>;
    saveMenuData: Reducer<{}>;
    // changeNotifyCount: Reducer<UserModelState>;
    // saveMenu:Reducer<UserModelState>;
  };
}

const UserModel: UserModelType = {
  namespace: 'user',

  state: {
    currentUser: {},
    menuData: [],
    isFetchMenu: false,
  },

  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchCurrent(_, { call, put }) {
      const response = yield call(queryCurrent);
      if (response.status === 'ok') {
        yield put({
          type: 'saveCurrentUser',
          payload: response.data,
        });
      } else {
        message.error(response.message);
        yield put(routerRedux.replace('/user/login' || '/'));
      }
    },
    *getMenuData({ payload, callback }, { put, call }) {
      const response = yield call(getAccountMenus);
      if (response.status === 'ok') {
        const menuData = response.data;
        yield put({
          type: 'saveMenuData',
          payload: menuData,
        });
      }
      // router.push('/exception/403');
      // yield put(routerRedux.replace('/exception/403' || '/'));

      if (callback && typeof callback === 'function') {
        callback(response); // 返回结果
      }
    },
  },

  reducers: {
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: action.payload || [],
      };
    },
    saveMenuData(state, action) {
      return {
        ...state,
        menuData: action.payload || {},
        isFetchMenu: true,
      };
    },
    // saveMenu(state, action) {
    //   console.log('saveMenu');
    //   console.log(state);
    //   return {
    //     ...state,
    //     ...action.payload,
    //   };
    // },
    // changeNotifyCount(
    //   state = {
    //     currentUser: {},
    //   },
    //   action,
    // ) {
    //   return {
    //     ...state,
    //     currentUser: {
    //       ...state.currentUser,
    //       // notifyCount: action.payload.totalCount,
    //       // unreadCount: action.payload.unreadCount,
    //     },
    //   };
    // },
  },
};

export default UserModel;
